package com.example.mapp;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.example.entity.supervision.SafeControlRecord;
import com.example.ext.entity.ExtSafeControlRecord;
import com.example.mapp.buildSql.SafeControlRecordMappSql;

/**
 * Created by douyx on 2018/03/26.
 */
@Mapper
public interface SafeControlRecordMapp {

	@Select(" select a.*, b.party_name as partyName,b.party_addr as partyAddr, b.corporation,b.contacts"
			+ ", b.contacts_work as contactsWork,b.contacts_phone as contactsPhone from safe_control_record a "
			+ " left join party b on a.used_party_id = b.id " + " where a.id=#{id}")
	ExtSafeControlRecord queryById(String id) throws Exception;

	@SelectProvider(type = SafeControlRecordMappSql.class, method = "queryList")
	List<ExtSafeControlRecord> queryList(ExtSafeControlRecord extSafeControlRecord) throws Exception;

	@Update(" update safe_control_record set advice=#{advice},advicer_id=#{advicerId}"
			+ ",advicer_name=#{advicerName},advicer_name_url=#{advicerNameUrl},advice_time=#{adviceTime},"
			+ " is_sure=#{isSure} where id=#{id} ")
	public int update(SafeControlRecord safeControlRecord) throws Exception;

	@Update(" update safe_control_record set check_name_url=#{checkNameUrl},checker_id=#{checkerId},"
			+ " checker_name=#{checkerName} where id=#{id} ")
	public int updateChecker(SafeControlRecord safeControlRecord) throws Exception;

	@Update(" update safe_control_record set law_doc_nos=#{lawDocNos},law_docs=#{lawDocs}, inspection_records_doc=#{inspectionRecordsDoc}"
			+ "  where id=#{id} ")
	public int uploadDocs(SafeControlRecord safeControlRecord) throws Exception;

	@Update(" update safe_control_record set "
			+ " punishment_docs=#{punishmentDocs},punishment_doc_nos=#{punishmentDocNos} where id=#{id} ")
	public int uploadPunDocs(SafeControlRecord safeControlRecord) throws Exception;

	@Update(" update safe_control_record set "
			+ " evidences=#{evidences} where id=#{id} ")
	public int uploadEvidences(SafeControlRecord safeControlRecord) throws Exception;

	@Insert(" insert into safe_control_record (check_type,check_time_from,check_time_to,used_party_id,check_item,device_name,device_num,questions,"
			+ "questions_url,deal_way,deal_remark,advice, advicer_id,advicer_name,advicer_name_url,advice_time,checker_id,checker_name,recorder_name," +
			" used_control_id,elevator_control_id,check_name_url, score," +
			" is_sure, state,p_detail_id,censor_type,law_doc_nos,law_docs,evidences,punishment_docs,recorder_id,ext1,ext2,program_name,entrust_ched_num,entrust_ched_name ) "
			+ " value "
			+ " (#{checkType},#{checkTimeFrom},#{checkTimeTo},#{usedPartyId},#{checkItem}," +
			"#{deviceName},#{deviceNum},#{questions},#{questionsUrl},#{dealWay},"
			+ "#{dealRemark},#{advice}, #{advicerId},#{advicerName},#{advicerNameUrl}," +
			"#{adviceTime},#{checkerId},#{checkerName},#{recorderName}," +
			"#{usedControlId},#{elevatorControlId},#{checkNameUrl},#{score},#{isSure},#{state},#{pDetailId}" +
			",#{censorType},#{lawDocNos},#{lawDocs},#{evidences},#{punishmentDocs},#{recorderId},#{ext1},#{ext2},#{programName},#{entrustChedNum},#{entrustChedName})")
	@SelectKey(statement = "select last_insert_id()", keyProperty = "id", keyColumn = "id", before = false, resultType = Long.class)
	public int add(SafeControlRecord safeControlRecord) throws Exception;

	@Select("SELECT DISTINCT CONCAT(C.USER_ID) userId FROM SAFE_CONTROL_RECORD A "
			+ " INNER JOIN PARTY_USER B ON A.USED_PARTY_ID=B.PARTY_ID "
			+ " INNER JOIN SYS_USER_ROLE C ON C.USER_ID=B.USER_ID "
			+ " INNER JOIN SYSTEM_ROLE D ON C.ROLE_ID=D.ID "
			+ " WHERE D.ROLE_NAME LIKE #{roleName} "
			+ " AND B.party_id = #{partyId}")
	List<String> queryUserList(Map<String, Object> map) throws Exception;
}
